package net.davidlauzon.assemblandroid.models;

import java.util.Date;

import android.util.Log;

public class Task {
	static public enum State {STARTED, PAUSED, STOPPED};
	
	static private final long MS_PER_HOUR   = 1000 * 60 * 60; // 1000ms * 60sec * 60min
	static private final long MS_PER_MINUTE = 1000 * 60;
	static private final long MS_PER_SECOND = 1000;
	
	int id = -1;
	Date beginAt = null;
	Date lastResumedAt = null;
	Date endAt = null;
	boolean billed = false;
	float hours = 0.0f;
	String description = null;
	String spaceId = null;
	int ticketId = -1;
	int ticketNumber = -1;
	String userId = null;
	State state;
	
	long elapsedTime = 0;
	
	// Constructor used when creating a new task from the timer
	public Task(String spaceId, int ticketId) {
		this.spaceId = spaceId;
		this.ticketId = ticketId;
	}
	
	// Constructor used by the XML Parser when reading an existing task
	public Task(int id, Date beginAt, Date endAt, float hours, String description, String spaceId, int ticketId, int ticketNumber, String userId) {
		this.id = id;
		this.beginAt = beginAt;
		this.endAt = endAt;
		this.hours = hours;
		this.description = description;
		this.spaceId = spaceId;
		this.ticketId = ticketId;
		this.ticketNumber = ticketNumber;
		this.userId = userId;
		this.state = State.STOPPED;
	}
	
	public String toString() {
		return "[Task #" + Integer.toString(id) + " " + state.toString() + "]" +
			" ; beginAt = " + (beginAt != null ? beginAt.toString() : "") + 
			" ; resumeAt = " + (lastResumedAt != null ? lastResumedAt.toString() : "") + 
			" ; endAt = " + (endAt != null ? endAt.toString() : "") + 
			" ; hours = " + hours +  
			" ; elapsedTime = " + Long.toString(elapsedMS()) + 
			" ; description = " + description;
	}
	
	
	public State state() { return this.state; }
	public float hours() { return this.hours; }
	
	public void start() {
		this.beginAt = this.lastResumedAt = new Date();
		this.state = State.STARTED;
	}
	
	public void pause() {
		Date now = new Date();
		this.elapsedTime += (now.getTime() - lastResumedAt.getTime());
		this.state = State.PAUSED;
	}
	
	public void resume() {
		this.lastResumedAt = new Date();
		this.state = State.STARTED;
	}
	
	public void stop() {
		this.endAt = new Date();
		this.elapsedTime += (endAt.getTime() - lastResumedAt.getTime());
		this.hours = elapsedHours();
		this.state = State.STOPPED;
	}

	/**
	 * @return live elapsed time in ms
	 */
	public long elapsedMS() {
		Date now = new Date();
		//Log.i("AssemblaTT", "[elapsedTime] Now = " + now.getTime());
		//return (float) this.hours + (now.getTime() - lastResumedAt.getTime()) / ONE_HOUR_BASE;
		
		if (state == State.STARTED) 
			return elapsedTime + (now.getTime() - lastResumedAt.getTime());
		else
			return elapsedTime;
	}
	
	public float elapsedHours() {
		return (float) elapsedMS() / MS_PER_HOUR;
	}
	
	public String elapsedTime() {
		long ms = elapsedMS();
		
		int hours   = (int) (ms / MS_PER_HOUR);
		int minutes = Math.round( (float) ((ms % MS_PER_HOUR) / MS_PER_MINUTE));
		int seconds = Math.round( (float) ((ms % MS_PER_MINUTE) / MS_PER_SECOND));
		
		return String.format("%d:%02d:%02d", hours, minutes, seconds);
	}

	public Date beginAt() {   return this.beginAt;  }
	public Date endAt() {   return this.endAt;  }

	public void setDescription(String description) {
		this.description = description;
	}

	public String description() {   return this.description;  }
}
